﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ADC.BL;
using ADC.DAL;

namespace ADC.Web.Admin
{
    public partial class AddContactUs : System.Web.UI.Page
    {
        log4net.ILog logger = log4net.LogManager.GetLogger("ContactUs");
        private int _aboutUsId;
        private int _mode;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                
                InitData();
            }
        }

        private void InitData()
        {
            if (Request.Params["ID"] != null && Request.Params["mode"] != null)
            {
                try
                {
                    //_aboutUsId = Convert.ToInt32(Request.Params["ID"].ToString());
                    _aboutUsId = 2; //lay ban ghi Lien he ve cong ty (contact us)
                    _mode = Convert.ToInt32(Request.Params["mode"].ToString());

                    if (_mode.Equals(1)) // Edit Mode
                    {
                        // Get from database
                        var aboutUs = AboutUsFactory.GetByID(_aboutUsId);
                        if (aboutUs != null)
                        {
                            txtTitle.Text = aboutUs.Title;
                            ucTextEditor1.TextValue = aboutUs.Description;
                        }
                    }
                }
                catch (Exception ex)
                {
                    _aboutUsId = 0;
                    _mode = 0;
                    logger.Error(ex.ToString());
                    if (GlobalInfo.IsEnableDebugMode) throw;
                }
            }
        }


        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                //var aboutUsID = Request.Params["ID"];
                var aboutUsID = 2;
                var aboutUs = new AboutU();
                aboutUs.Title = txtTitle.Text;
                aboutUs.Description = ucTextEditor1.TextValue;
                aboutUs.ModifiedByID = ((User)Session["UserInfo"]).UserID;
                if (aboutUsID != null)
                {
                    try
                    {
                        aboutUs.AboutUsID = Convert.ToInt32(aboutUsID);
                    }
                    catch (Exception)
                    {
                    }
                }

                if (AboutUsFactory.Update(aboutUs) > 0)
                {
                    lblErrorMessage.Visible = true;
                    lblErrorMessage.Text = "Cập nhật thành công";
                }
                else
                {
                    lblErrorMessage.Visible = true;
                    lblErrorMessage.Text = "Cập nhật không thành công";
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                if (GlobalInfo.IsEnableDebugMode) throw;
            }
        }
    }
}